package xmlrpc.server;

import java.io.IOException;

import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.server.PropertyHandlerMapping;
import org.apache.xmlrpc.server.XmlRpcServer;
import org.apache.xmlrpc.webserver.WebServer;



/**
 * Main class for the server.
 * 
 * @author Qw4z1
 * 
 */

public class ConServer {
	private WebServer server;
	private StringSenderInterface stringSender = new StringSender();

	public ConServer() {

	}
	/**
	 * 
	 * @param port serverport
	 */
	public void startServer(int port) {

		// start server

		server = new WebServer(port);
		
		//get XmlRpcServer object and propertyhandler mapping
		XmlRpcServer xmlRpcServer = server.getXmlRpcServer();
		PropertyHandlerMapping phm = new PropertyHandlerMapping();

		phm.setRequestProcessorFactoryFactory(new ConProcessorFactoryFactory(stringSender));
		phm.setVoidMethodEnabled(true);
		
		try {
			phm.addHandler("StringSender", xmlrpc.server.StringSender.class);

		} catch (XmlRpcException e) {
			e.printStackTrace();
		}
		xmlRpcServer.setHandlerMapping(phm);

		try {
			server.start();
			System.out.println("Server running on port " + port);
		} catch (IOException e) {
			System.out.println("Failed to start server!");
			e.printStackTrace();
		}
	}

	public void stopServer() {
		server.shutdown();
	}

}
